<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;

class buycar extends controller
{
   
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
       !empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid');
        $user = Db::table('s75_user')->field(['icon','name','id'])->where('id',$id)->find();
        $list = Db::field('d.id as did,d.name,d.face,o.pay_money,d.author,o.id')
                ->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=0')->where('o.uid='.$id)
                ->select();
        $vip = db('user')->where('id',$uid)->where('vip',1)->find();
        $this->assign('vip',$vip);
        $this->assign('count',count($list));
        $this->assign('list',$list);
        $this->assign('user',$user);
        return $this->fetch('buycar/index');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('s75_orders')->where('id',$id)->delete(); 
          if($result) {
            $info['status'] = 'true';
            $info['info'] = '删除成功';
          }else{
            $info['status'] = 'false';
            $info['info'] = '删除失败';
          }
          return json($info); 
    }


    public function showinfo($id)
    {
        // 是否为 Ajax 请求
        if (!Request::instance()->isAjax()) {
            $this->error('不是Ajax请求', '/');
        }
        // 显式查询
        $row = Db::table('s75_orders')->field(['pay_money'])->find($id);
        $info['status'] = true;
        $info['money'] = $row['pay_money'];
        $info['count'] = count($row);
        return json($info);
    }

    public function deletemore(Request $request)
    {
        $data = $request->post();
        $id = $data['id'];
        $result = Db::table('s75_orders')->where('id','in',$id)->delete();
          if($result) {
            $info['status'] = 'true';
            $info['info'] = '删除成功';
          }else{
            $info['status'] = 'false';
            $info['info'] = '删除失败';
          }
          return json($info); 
    }

    public function gotopay(Request $request)
    {
      !empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid');
      $res = $request->post();
      $id = $res['id'];
      $data = Db::table('s75_orders')->where('id','in',$id)->where('status',1)->where('uid',$uid)->find();
      if(!$data){
                   $result = Db::table('s75_orders')->where('id','in',$id)->where('uid',$uid)->update(['status'=>1,'otime'=>time()]);
                   $info['status'] = 'true';
                   $info['info'] = '购买成功';
        }else{
               $info['status'] = 'false';
               $info['info'] = '你已经购买过啦'; 
        }
        return json($info); 
    }

}
